package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jxs.android.util.DetainUtils;
import com.jxs.android.weiget.BuLoadingDailog;
import com.telecom.weixi.R;

/**
 * @Des 用户
 * @Author jiangxs
 * @Des 2012-11-21 上午9:25:54
 */
public abstract class MyWork_VIP_Laucher_Abstract extends LoadingMoreAbstractList {

	private String orderFlag = "1";// 排序标示，1入网日期，2合账用户数，3账户ARPU，4账户余额
	private int REQ = 0x9322;

	private WrkUserAdapter adapter;
	private SortBroadCastReciver receiver = new SortBroadCastReciver();

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(receiver, new IntentFilter(MyLaucher.SORT_ACTION));
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_list_no_top_bar;
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), Which.MY_WORK_VIP, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, getTabId(), orderFlag, PAGE_SIZE, currentPage);
	}

	protected abstract String getTabId();

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new WrkUserAdapter(context);
		return adapter;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ && resultCode == RESULT_OK) {

			if (DetainUtils.isRemove()) {
				adapter.removeWrkUser(data.getExtras().getInt("position"));
				adapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<WrkUser> tems = new ArrayList<WrkUser>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				WrkUser cstInfo = new WrkUser();
				if (taskBeanObject.hasProperty("cust_id")) {
					cstInfo.cust_id = taskBeanObject.getPropertySafelyAsString("cust_id");// 用户编码
				}
				cstInfo.serv_id = taskBeanObject.getPropertySafelyAsString("serv_id");// 用户编码
				cstInfo.acc_nbr = taskBeanObject.getPropertySafelyAsString("acc_nbr");// 电话号码
				cstInfo.cust_type_name = taskBeanObject.getPropertySafelyAsString("cust_type_name");// 客户类型
				cstInfo.innet_dur = taskBeanObject.getPropertySafelyAsString("innet_dur");// 在网时长
				cstInfo.owed_halt = taskBeanObject.getPropertySafelyAsString("owed_halt");// 欠费状态
				cstInfo.serv_name = taskBeanObject.getPropertySafelyAsString("serv_name");// 用户名称
				cstInfo.cust_sub_brand_name = taskBeanObject.getPropertySafelyAsString("cust_sub_brand_name");// 客户品牌
				cstInfo.acctarpu = taskBeanObject.getPropertySafelyAsString("acctarpu");// 账户ARPU
				cstInfo.join_acct_serv_num = taskBeanObject.getPropertySafelyAsString("join_acct_serv_num");// 合帐用户数
				cstInfo.cdma_product_num = taskBeanObject.getPropertySafelyAsString("cdma_product_num");// 手机用户数
				cstInfo.fix_num = taskBeanObject.getPropertySafelyAsString("fix_num");// 固话用户数
				cstInfo.brd_num = taskBeanObject.getPropertySafelyAsString("brd_num");// 宽带用户数
				cstInfo.other_product_num = taskBeanObject.getPropertySafelyAsString("other_product_num");// 其他产品用户数
				cstInfo.detain_info = taskBeanObject.getPropertySafelyAsString("detain_info");// 维系信息
				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<WrkUser> tems = (List<WrkUser>) msg.obj;
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				refreshListView.removeFooterView(loadMoreView);
			}
		}
		adapter.setWrkUsers(tems, currentPage);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}

	}

	class WrkUserAdapter extends BaseAdapter {

		List<WrkUser> custInfos = new ArrayList<WrkUser>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public WrkUserAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setWrkUsers(List<WrkUser> custInfos, int currentPage) {
			if (currentPage == 1) {
				this.custInfos.clear();
			}
			this.custInfos.addAll(custInfos);
		}

		public void removeWrkUser(int position) {
			this.custInfos.remove(position);
		}

		@Override
		public int getCount() {
			return custInfos.size();
		}

		@Override
		public WrkUser getItem(int position) {
			return custInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final WrkUser custInfo = custInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_work_user, null);
				holder = new ViewHolder();

				holder.serv_id = (TextView) convertView.findViewById(R.id.serv_id);// 用户编码
				holder.acc_nbr = (TextView) convertView.findViewById(R.id.acc_nbr);// 电话号码
				holder.cust_type_name = (TextView) convertView.findViewById(R.id.cust_type_name);// 客户类型
				holder.innet_dur = (TextView) convertView.findViewById(R.id.innet_dur);// 在网时长
				holder.owed_halt = (TextView) convertView.findViewById(R.id.owed_halt);// 欠费状态
				holder.serv_name = (TextView) convertView.findViewById(R.id.serv_name);// 用户名称
				holder.cust_sub_brand_name = (TextView) convertView.findViewById(R.id.cust_sub_brand_name);// 客户品牌
				holder.acctarpu = (TextView) convertView.findViewById(R.id.acctarpu);// 账户ARPU
				holder.join_acct_serv_num_detail = (TextView) convertView.findViewById(R.id.join_acct_serv_num_detail);// 合帐用户数
				holder.detain_info = (TextView) convertView.findViewById(R.id.detain_info);// 维系信息

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.serv_id.setText(custInfo.serv_id);// 用户编码
			holder.acc_nbr.setText(custInfo.acc_nbr);// 电话号码
			holder.cust_type_name.setText(custInfo.cust_type_name);// 客户类型
			holder.innet_dur.setText(Html.fromHtml(custInfo.innet_dur));// 在网时长
			holder.owed_halt.setText(custInfo.owed_halt);// 欠费状态
			holder.serv_name.setText(custInfo.serv_name);// 用户名称
			holder.cust_sub_brand_name.setText(custInfo.cust_sub_brand_name);// 客户品牌
			holder.acctarpu.setText(custInfo.acctarpu);// 账户ARPU
			holder.join_acct_serv_num_detail.setText(custInfo.join_acct_serv_num + " [ CDMA:" + custInfo.cdma_product_num + ", 固话:" + custInfo.fix_num + ",带宽:" + custInfo.brd_num
			        + ",其他:" + custInfo.other_product_num + " ] ");// 合帐用户数
			holder.detain_info.setText(custInfo.detain_info);// 维系信息

			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View paramView) {

					Intent intent = new Intent();

					Bundle bundle = new Bundle();
					bundle.putString("serv_id", custInfo.serv_id);
					bundle.putInt("position", position);

					bundle.putString("name_label", getString(R.string.serv_name));
					bundle.putString("name_value", custInfo.serv_name);
					bundle.putString("phone_label", getString(R.string.acc_nbr));
					bundle.putString("phone_value", custInfo.acc_nbr);
					bundle.putString("detain_label", getString(R.string.detain_info));
					bundle.putString("detain_value", custInfo.detain_info);

					if ("1".equals(getTabId())) {
						bundle.putString("title", "流失预警维系执行");
						intent.putExtras(bundle);
						intent.setClass(MyWork_VIP_Laucher_Abstract.this, MyWork_Common_Losed_Detain.class);
						startActivityForResult(intent, REQ);
					} else if ("2".equals(getTabId())) {
						bundle.putString("title", "价值提升维系执行");
						intent.setClass(MyWork_VIP_Laucher_Abstract.this, MyWork_Common_Biz_Detain.class);
						intent.putExtras(bundle);
						startActivityForResult(intent, REQ);
					}
				}
			});

			return convertView;
		}

		class ViewHolder {
			TextView serv_id;// 用户编码
			TextView acc_nbr;// 电话号码
			TextView cust_type_name;// 客户类型
			TextView innet_dur;// 在网时长
			TextView owed_halt;// 欠费状态
			TextView serv_name;// 用户名称
			TextView cust_sub_brand_name;// 客户品牌
			TextView acctarpu;// 账户ARPU
			TextView join_acct_serv_num_detail;// 合帐用户数
			TextView cdma_product_num;// 手机用户数
			TextView fix_num;// 固话用户数
			TextView brd_num;// 宽带用户数
			TextView other_product_num;// 其他产品用户数
			TextView detain_info;// 维系信息
		}

	}

	class WrkUser {
		String serv_id;// 用户编码
		String cust_id = "0";// cust_id
		String acc_nbr;// 电话号码
		String cust_type_name;// 客户类型
		String innet_dur;// 在网时长
		String owed_halt;// 欠费状态
		String serv_name;// 用户名称
		String cust_sub_brand_name;// 客户品牌
		String acctarpu;// 账户ARPU
		String join_acct_serv_num;// 合帐用户数
		String cdma_product_num;// 手机用户数
		String fix_num;// 固话用户数
		String brd_num;// 宽带用户数
		String other_product_num;// 其他产品用户数
		String detain_info;// 维系信息

	}

	@Override
	public String getMethod() {
		return "getDetainServVipPage";
	}

	private class SortBroadCastReciver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (null != intent && null != intent.getExtras()) {
				orderFlag = intent.getExtras().getString("sortkey");
				currentPage = 1;
				loadMoreButton.setEnabled(true);
				loadMoreButton.setText("正在加载中..."); // 设置按钮文字
				loadingData(currentPage, true);
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}

}
